package com.junbao.androidinterview.net.interceptor;

import androidx.annotation.NonNull;

import com.junbao.androidinterview.utils.LogUtils;

import java.io.IOException;
import java.util.logging.Logger;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * author : Majunbao
 * github : https://github.com/MaJunBaox
 * time   : 2022/8/7 6:58 下午
 * desc   : 日志拦截器
 */
public class LoggerInterceptor implements Interceptor {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        //获取请求
        Request request = chain.request();
        long t1 = System.nanoTime();
        //打印请求的Url ，连接 ，请求头
        LogUtils.d(String.format("sending request %s on %s%n%s",request.url(),chain.connection(),request.headers()));
        Response response = chain.proceed(request);
        long t2 = System.nanoTime();
        //打印执行对的请求的URL ， 消耗时间，响应头
        LogUtils.d(String.format("Received response for %s in %s %n %s" , response.request().url() , (t2 - t1)  , response.headers()));
        return response;
    }
}
